import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/user-or-role',
    component: Layout,
    redirect: '/user-or-role/user',
    name: 'UserOrRole',
    meta: {
      title: '用户角色',
      icon: 'excel'
    },
    children: [
      {
        path: 'user',
        component: () => import('@/views/user-or-role/user'),
        name: 'User',
        meta: { title: '用户管理' }
      },
      {
        path: 'online',
        component: () => import('@/views/user-or-role/online'),
        name: 'Online',
        meta: { title: '在线用户' }
      },
      {
        path: 'role',
        component: () => import('@/views/user-or-role/role'),
        name: 'Role',
        meta: { title: '角色管理' }
      }
    ]
  },
  {
    path: '/dept-or-job',
    component: Layout,
    redirect: '/dept-or-job/dept',
    name: 'DeptOrJob',
    meta: {
      title: '部门岗位',
      icon: 'excel'
    },
    children: [
      {
        path: 'dept',
        component: () => import('@/views/dept-or-job/dept'),
        name: 'Dept',
        meta: { title: '部门管理' }
      },
      {
        path: 'job',
        component: () => import('@/views/dept-or-job/job'),
        name: 'Job',
        meta: { title: '岗位管理' }
      }
    ]
  },
  {
    path: '/func-or-menu',
    component: Layout,
    redirect: '/func-or-menu/menu',
    name: 'FuncOrMenu',
    meta: {
      title: '功能菜单',
      icon: 'excel'
    },
    children: [
      {
        path: 'func',
        component: () => import('@/views/func-or-menu/func'),
        name: 'Func',
        meta: { title: '前台功能' }
      },
      {
        path: 'menu',
        component: () => import('@/views/func-or-menu/menu'),
        name: 'Menu',
        meta: { title: '后台菜单' }
      }
    ]
  },
  {
    path: '/income-or-expenses',
    component: Layout,
    redirect: '/income-or-expenses/income',
    name: 'IncomeOrExpenses',
    meta: {
      title: '收支管理',
      icon: 'excel'
    },
    children: [
      {
        path: 'income',
        component: () => import('@/views/income-or-expenses/income'),
        name: 'Income',
        meta: { title: '收入管理' }
      },
      {
        path: 'expenses',
        component: () => import('@/views/income-or-expenses/expenses'),
        name: 'Expenses',
        meta: { title: '支出管理' }
      }
    ]
  },
  {
    path: '/dev-ops',
    component: Layout,
    redirect: '/dev-ops/customer-log',
    name: 'DevOps',
    meta: {
      title: '运维管理',
      icon: 'excel'
    },
    children: [
      {
        path: 'customer-log',
        component: () => import('@/views/dev-ops/customer-log'),
        name: 'CustomerLog',
        meta: { title: '前台日志' }
      },
      {
        path: 'admin-log',
        component: () => import('@/views/dev-ops/admin-log'),
        name: 'AdminLog',
        meta: { title: '后台日志' }
      },
      {
        path: 'xxl-job',
        component: () => import('@/views/dev-ops/xxl-job'),
        name: 'XxlJob',
        meta: { title: '定时任务' }
      },
      {
        path: 'swagger',
        component: () => import('@/views/dev-ops/swagger'),
        name: 'Swagger',
        meta: { title: '接口文档' }
      }
    ]
  },
  {
    path: '/service-monitor',
    component: Layout,
    redirect: '/service-monitor/system-monitor',
    name: 'ServiceMonitor',
    meta: {
      title: '服务监控',
      icon: 'excel'
    },
    children: [
      {
        path: 'system-monitor',
        component: () => import('@/views/service-monitor/system-monitor'),
        name: 'SystemMonitor',
        meta: { title: '系统监控' }
      },
      {
        path: 'sql-monitor',
        component: () => import('@/views/service-monitor/sql-monitor'),
        name: 'SqlMonitor',
        meta: { title: 'SQL监控' }
      },
      {
        path: 'nacos',
        component: () => import('@/views/service-monitor/nacos'),
        name: 'Nacos',
        meta: { title: 'Nacos' }
      },
      {
        path: 'sentinel',
        component: () => import('@/views/service-monitor/sentinel'),
        name: 'Sentinel',
        meta: { title: 'Sentinel' }
      },
      {
        path: 'spring-boot-monitor',
        component: () => import('@/views/service-monitor/spring-boot-monitor'),
        name: 'SpringBootMonitor',
        meta: { title: 'Spring Boot Monitor' }
      },
      {
        path: 'mq-monitor',
        component: () => import('@/views/service-monitor/mq-monitor'),
        name: 'MQMonitor',
        meta: { title: 'MQ监控' }
      }
    ]
  },
  {
    path: '/plug-manage',
    component: Layout,
    redirect: '/plug-manage/email-manage',
    name: 'PlugManage',
    meta: {
      title: '插件管理',
      icon: 'excel'
    },
    children: [
      {
        path: 'email-manage',
        component: () => import('@/views/plug-manage/email-manage'),
        name: 'EmailManage',
        meta: { title: '邮件管理' }
      },
      {
        path: 'sms-manage',
        component: () => import('@/views/plug-manage/sms-manage'),
        name: 'SMSManage',
        meta: { title: '短信管理' }
      },
      {
        path: 'local-storage',
        component: () => import('@/views/plug-manage/local-storage'),
        name: 'LocalStorage',
        meta: { title: '本地存储' }
      },
      {
        path: 'cloud-storage',
        component: () => import('@/views/plug-manage/cloud-storage'),
        name: 'CloudStorage',
        meta: { title: '云存储' }
      }
    ]
  },
  {
    path: '/center',
    component: Layout,
    redirect: '/center/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/center/index'),
        name: 'Center',
        meta: { title: '个人中心', icon: 'user', noCache: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
